﻿using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;

namespace WimClipboard
{
    /// <summary>
    /// Logique d'interaction pour MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private MainWindowViewModel viewModel;

        public MainWindow()
        {
            InitializeComponent();
            this.Loaded += this.OnMainWindowLoaded;
        }

        private void OnMainWindowLoaded(object sender, RoutedEventArgs e)
        {
            this.Loaded -= this.OnMainWindowLoaded;
            this.viewModel = new MainWindowViewModel();
            this.viewModel.PropertyChanged += this.OnViewModelPropertyChanged;
            this.DataContext = this.viewModel;
            this.ResetClipboardContent();
        }

        private void OnViewModelPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            this.ResetClipboardContent();
        }

        private void ResetClipboardContent()
        {
            this.DataTabControl.Items.Clear();

            foreach (var format in this.viewModel.DataObjectFormats)
            {
                this.DataTabControl.Items.Add(this.GetTab(format));
            }
        }

        private TabItem GetTab(string format)
        {
            TabItem tabItem;
            var data = Clipboard.GetData(format);

            if (format == KnownFormats.RichTextFormat)
            {
                tabItem = GetRichTextTab(format, data);
            }
            else if (format == KnownFormats.HTMLFormat)
            {
                tabItem = GetHTMLTab(format, data);
            }
            else
            {
                tabItem = GetTextTab(format, data);
            }

            return tabItem;
        }

        private static TabItem GetTextTab(string format, object data)
        {
            TabItem tabItem;
            tabItem = new TabItem()
            {
                Header = format,
                Content = new TextBox()
                {
                    IsReadOnly = true,
                    HorizontalScrollBarVisibility = ScrollBarVisibility.Auto,
                    VerticalScrollBarVisibility = ScrollBarVisibility.Auto,
                    Text = data == null ? "null" : data.ToString()
                }
            };
            return tabItem;
        }

        private static TabItem GetHTMLTab(string format, object data)
        {
            TabItem tabItem;
            var text = data == null ? "null" : data.ToString();

            var stackPanel = new StackPanel();
            stackPanel.SetValue(Grid.RowProperty, 1);
            stackPanel.Orientation = Orientation.Horizontal;

            var lines = text.Split(new[] { "\r\n" }, StringSplitOptions.None);

            foreach (var line in lines)
            {
                if (line[0] == '<')
                {
                    break;
                }
                else
                {
                    text = text.Substring(line.Length + 2);
                    stackPanel.Children.Add(new TextBlock() { Text = line, Margin = new Thickness(0, 0, 10, 0) });
                }
            }

            var browser = new WebBrowser();
            browser.SetValue(Grid.RowProperty, 0);
            browser.NavigateToString(text);

            var grid = new Grid();
            grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
            grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
            grid.Children.Add(stackPanel);
            grid.Children.Add(browser);

            tabItem = new TabItem()
            {
                Header = format,
                Content = grid
            };
            return tabItem;
        }

        private static TabItem GetRichTextTab(string format, object data)
        {
            TabItem tabItem;
            var text = data == null ? "null" : data.ToString();

            MemoryStream stream = new MemoryStream(ASCIIEncoding.Default.GetBytes(text));
            var richTextBox = new RichTextBox()
            {
                IsReadOnly = true,
                HorizontalScrollBarVisibility = ScrollBarVisibility.Auto,
                VerticalScrollBarVisibility = ScrollBarVisibility.Auto
            };
            richTextBox.Document.MinPageWidth = 1000;

            var RTBText = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
            RTBText.Load(stream, DataFormats.Rtf);

            tabItem = new TabItem()
            {
                Header = format,
                Content = richTextBox
            };

            return tabItem;
        }
    }
}